/**
 * @file StreamPipe.h
 *
 * This file defines the StreamPipe API
 *
 * @author Luc Fourestier
 * @date 2009
 */


#ifndef STREAMPIPE_H_
#define STREAMPIPE_H_

#include "Global.h"

#include "StreamAnalyser.h"
#include "StreamReader.h"
#include "StreamPlayer.h"

#include <stdio.h>
#include <sys/time.h>

// Defines
#define SPI_MAX_NAME_LENGTH MAX_NAME_LENGTH

// Errors
/**
 * Defines the StreamPipe errors
 */
typedef enum SPIErrorType_e
{
	SPI_ERROR_BAD_PARAM,       		//!< Incorrect parameters
	SPI_ERROR_CANNOT_ALLOCATE, 		//!< Cannot create/allocate object required by the StreamPipe
	SPI_ERROR_CANNOT_CREATE_SA,  	//!< Cannot create StreamAnalyzer
	SPI_ERROR_CANNOT_CREATE_SR,  	//!< Cannot create StreamReader
	SPI_ERROR_CANNOT_CREATE_SP,  	//!< Cannot create StreamPlayer
	SPI_ERROR_EMPTY_LIST,  			//!< The StreamPipe list is empty
} SPIErrorType_t;

/**
 * Session type enumeration
 */
typedef enum SessionType_e
{
	Record,		//!< Record from network to a file
	Playback, 	//!< Playback from a file to network
} SessionType_t;

/**
 * The options to create a StreamPipe
 */
typedef struct SPI_Option_s
{
	SessionType_t SessionType;					//!< The session type
	char SessionID[SPI_MAX_NAME_LENGTH];  		//!< The session ID string
	char FileName[SR_MAX_FILE][SPI_MAX_NAME_LENGTH];  		//!< The name of the file(s)
	char Address[IP_ADDR_LENGTH];  				//!< The in/out address
	unsigned int Port;  				    	//!< The in/out port
	char OwnAddress[IP_ADDR_LENGTH];  			//!< Our own address
	unsigned int MaxCBSize;						//!< The circular buffer size (number of element)
	struct timeval ReferenceTime; 				//!< The reference time for timestamps
} SPI_Option_t;

/**
 * The StreamPipe info structure
 */
typedef struct SPI_StreamPipe_s
{
	unsigned char Magic; 						//!< Magic number (internal use)
	SPI_Option_t Options; 						//!< Init options
	SA_StreamAnalyzer_t * SA;					//!< The StreamAnalyzer
	SR_StreamReader_t * SR;						//!< The StreamReader
	SP_StreamPlayer_t * SP;						//!< The StreamPlayer
	struct SPI_StreamPipe_s * Previous;			//!< The previous StreamPipe in the list
	struct SPI_StreamPipe_s * Next;				//!< The next StreamPipe in the list
} SPI_StreamPipe_t;


/**
 * @brief Create a StreamPipe
 */
int SPI_Create(SPI_StreamPipe_t ** SPI_p, SPI_Option_t Options);

/**
 * @brief Delete a StreamPipe by SPI_StreamPipe_t
 */
int SPI_Delete(SPI_StreamPipe_t * SPI);

/**
 * @brief Delete a StreamPipe by ID
 */
int SPI_DeleteID(char * SessionID);

/**
 * @brief Delete the SPI list
 */
int SPI_DeleteList(void);

/**
 * @brief List all the StreamPipe
 */
int SPI_List(SPI_StreamPipe_t ** SPITable, unsigned int * Size_p);

/**
 * @brief Print the StreamPipe info by SPI_StreamPipe_t
 */
int SPI_Print(SPI_StreamPipe_t * SPI, int Level, FILE * Output);

/**
 * @brief Print the StreamPipe info by ID
 */
int SPI_PrintID(char * SessionID, int Level, FILE * Output);

/**
 * @brief Print the list
 */
int SPI_PrintList(int Level, FILE * Output);

/**
 * Search a StreamPipe by sessionID
 */
int SPI_Search(char * SessionID, SPI_StreamPipe_t ** SPI_p);

#endif /* STREAMPIPE_H_ */
